<template>
  <div class="login-page">
    <el-card class="el-card">
      <template #header>黑马面经运营后台</template>
      <el-form ref="form" :model="userForm" :rules="rules" autocomplete="off" class="el-form">

        <el-form-item prop="username" label="用户名: ">
          <el-input v-model="userForm.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item prop="password" label="密码：">
          <el-input type="password" v-model="userForm.password" placeholder="请输入密码：" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>

          <el-button @click="reset">重置</el-button>

        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'LoginIndex',
  data () {
    return {
      userForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          // trigger-触发时机 blur-失去焦点 message-提示内容 required-必填
          { required: true, message: '请输入活动名称', trigger: 'change' },
          { min: 3, max: 5, message: '长度在3-5之间', trigger: 'change' }
        ],
        password: [
          // \d 数字 0-9
          // \w 字母数字下划线
          // {m,n} 前面的字符，可以出现 m次 ~ n次
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        await this.$store.dispatch('user/loginAction', this.userForm)
        this.$router.push('/')
      })
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
// 组件内部的元素样式不生效的
// 默认在scoped作用域下，添加的样式只会影响到当前组件模板的内容
// 深度作用选择器(第三方组件): 向下/深度的影响到后代元素的样式
// less /deep/
// scss ::v-deep
.el-card {
  width: 500px;
  margin: 0 auto;
  ::v-deep .el-card__header {
    text-align: center;
    background-color: #6973ef;
    color: #fff;
  }
}
.el-form {
  padding: 0 20px;
}
</style>
